package manage.controller;

import core.controller.BaseController;
import core.domain.ListDataInfo;
import core.domain.Result;
import lombok.extern.slf4j.Slf4j;
import manage.domain.exam.dto.ExamEditDTO;
import manage.domain.exam.dto.ExamListDTO;
import manage.domain.exam.dto.ExamAddDTO;
import manage.domain.exam.dto.ExamQuestionAddDTO;
import manage.domain.exam.vo.ExamDetailVO;
import manage.domain.exam.vo.ExamVO;
import manage.service.IExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/exam")
public class ExamController extends BaseController {

    @Autowired
    private IExamService examService;
    @GetMapping("/list")
    public ListDataInfo list(ExamListDTO examListDTO){
        log.info("ExamController -> list: {}", examListDTO);
        List<ExamVO> list = examService.list(examListDTO);
        return toListDataInfo(list);
    }


    @PostMapping("/add")
    public Result<String> add(@RequestBody @Validated ExamAddDTO examAddDTO){
        log.info("ExamController -> add: {}", examAddDTO);
        return Result.success(examService.add(examAddDTO));
    }

    @GetMapping("/detail")
    public Result<ExamDetailVO> detail(Long examId){
        log.info("ExamController -> detail: {}", examId);

        return Result.success(examService.detail(examId));
    }

    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody @Validated ExamEditDTO examEditDTO) {
        log.info("ExamController -> update: {}", examEditDTO);
        return toResult(examService.edit(examEditDTO));
    }

    @DeleteMapping("/delete")
    public Result<Void> delete(Long examId) {
        log.info("ExamController -> delete: {}", examId);
        return toResult(examService.delete(examId));
    }

    @PostMapping("/question/add")
    public Result<Void> addQuestion(@RequestBody @Validated ExamQuestionAddDTO examQuestionAddDTO){
        log.info("ExamController -> addQuestion: {}", examQuestionAddDTO);
        return toResult(examService.addQuestion(examQuestionAddDTO));
    }
    @DeleteMapping("/question/delete")
    public Result<Void> deleteQuestion(Long examId, Long questionId) {
        log.info("ExamController -> delete: {}", examId);
        return toResult(examService.deleteQuestion(examId, questionId));
    }


    @PutMapping("/publish")
    public Result<Void> publish(Long examId) {
        log.info("ExamController -> publish: {}", examId);
        return toResult(examService.publish(examId));
    }

    @PutMapping("/cancelPublish")
    public Result<Void> unPublish(Long examId) {
        log.info("ExamController -> unPublish: {}", examId);
        return toResult(examService.unPublish(examId));
    }
}
